import {
  CHANGE_INPUT_VALUE,
  ADD_ITEM,
  DELETE_TODO_ITEM,
  INI_LIST,
  GET_INIT_LIST,
} from './actionCreators'

const defaultState = {
  inputValue: '',
  list: []
}

export default (state = defaultState, action) => {
  switch(action.type){
    case CHANGE_INPUT_VALUE:
    return {
      ...state,
      inputValue: action.value
    }
    case ADD_ITEM:
    return {
      inputValue: '',
      list : [...state.list, state.inputValue]
    }
    case DELETE_TODO_ITEM:{
      // const newState = JSON.parse(JSON.stringify(state))
      // newState.list.splice(action.index,1)
      // return newState
      const newState = [...state.list]
      newState.splice(action.index,1)
      return {
        ...state,
        list : newState
      }
    }
    case GET_INIT_LIST:
    return {
      ...state,
      list: action.list
    }
    default:
    return state
  }
}
